package org.ielse.frame.view;

import org.ielse.frame.util.BaseUtil;
import org.ielse.frame.util.LogUtil;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TitleLayout extends LinearLayout {

	private DisplayMetrics outMetrics = new DisplayMetrics();

	private FrameLayout layCenter, layLeft, layRight;

	public TitleLayout(Context context) {
		this(context, null);
	}

	public TitleLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(outMetrics);

		setOrientation(HORIZONTAL);
		setBackgroundColor(0xf02bbea1);
		LayoutParams lpLeft = new LayoutParams(0, LayoutParams.MATCH_PARENT);
		lpLeft.weight = 1;
		layLeft = new FrameLayout(context);
		layLeft.setLayoutParams(lpLeft);
		addView(layLeft);
		layCenter = new FrameLayout(context);
		LayoutParams lpCenter = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
		layCenter.setLayoutParams(lpCenter);
		addView(layCenter);
		LayoutParams lpRight = new LayoutParams(0, LayoutParams.MATCH_PARENT);
		lpRight.weight = 1;
		layRight = new FrameLayout(context);
		layRight.setLayoutParams(lpRight);
		addView(layRight);

	}

	@Override protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		LogUtil.d("title onLayout");
		final int heightSize = BaseUtil.isScreenVertical((Activity) getContext()) ? (int) (outMetrics.widthPixels * 0.135f)
				: (int) (outMetrics.heightPixels * 0.135f);
		getLayoutParams().height = heightSize;
	}

	public void setTextTitle(int textId) {
		TextView txTitle = new TextView(getContext());
		FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lp.gravity = Gravity.CENTER;
		txTitle.setLayoutParams(lp);
		txTitle.setTextSize(18);
		txTitle.setText(textId);
		txTitle.setTextColor(Color.WHITE);
		layCenter.removeAllViews();
		layCenter.addView(txTitle);
	}

	public void setRightView(View view) {
		layRight.removeAllViewsInLayout();
		layRight.addView(view);
	}

}
